/**
 * 
 */
package com.binhthuan.catechist.client.grid;

import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.ECIcons;
import com.binhthuan.catechist.client.ECUser;
import com.binhthuan.catechist.client.data.TraineeDS;
import com.binhthuan.catechist.shared.constant.Action;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * @author dttgiang
 * 
 */
public class TraineeGrid extends AbstractGrid {

    /**
     * @param dataSource
     */
    public TraineeGrid(TraineeDS dataSource) {
        super(dataSource);

        ListGridField id = new ListGridField(Parameter.ID);
        id.setHidden(true);
        id.setCanHide(false);

        ListGridField active = new ListGridField(Parameter.ACTIVE);
        active.setTitle(" ");
        active.setType(ListGridFieldType.BOOLEAN);
        active.setWidth(24);

        ListGridField patronSaint = new ListGridField(Parameter.PATRON_SAINT);
        patronSaint.setTitle(ECApplication.getConstants().patronSaint());
        patronSaint.setWidth(60);

        ListGridField lastName = new ListGridField(Parameter.LAST_NAME);
        lastName.setTitle(ECApplication.getConstants().lastName());

        ListGridField firstName = new ListGridField(Parameter.FIRST_NAME);
        firstName.setTitle(ECApplication.getConstants().firstName());

        ListGridField birthday = new ListGridField(Parameter.BIRTHDAY);
        birthday.setTitle(ECApplication.getConstants().birthday());

        ListGridField gender = new ListGridField(Parameter.MALE);
        gender.setTitle(" ");
        gender.setCellFormatter(new CellFormatter() {

            @Override
            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                boolean isMale = (value == null) ? false : Boolean.valueOf(value.toString());
                return Canvas.imgHTML(isMale ? ECIcons.getIcon("male.png") : ECIcons.getIcon("female.png"), 16, 16);
            }
        });
        gender.setWidth(20);

        ListGridField phone = new ListGridField(Parameter.PHONE);
        phone.setTitle(ECApplication.getConstants().phone());

        setFields(id, active, patronSaint, lastName, firstName, gender, birthday, phone);
        setBooleanTrueImage(ECIcons.getIcon("active.png"));
        setBooleanFalseImage(ECIcons.getIcon("inactive.png"));
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.binhthuan.catechist.client.grid.AbstractGrid#initializeCriteria()
     */
    @Override
    protected Criteria initializeCriteria() {
        Criteria initCrit = new Criteria(Parameter.OPERATION_INFO, Action.GET_TRAINEES_BY_COURSE);
        initCrit.addCriteria(Parameter.COURSE, ECUser.getCurrentCourse());

        return initCrit;
    }
}
